package com.cy.ppj.service.mall.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.mall.BrandMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.mall.Brand;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.BrandAdminListQuery;
import com.cy.ppj.model.vo.mall.BrandAdminDetailVO;
import com.cy.ppj.model.vo.mall.BrandAdminListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.BrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 品牌 service
 * @Author tanyilin
 * @Date 2019/7/2 15:06
 */
@Service
public class BrandServiceImpl extends BaseService implements BrandService {

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private FileService fileService;


    @Override
    public PageBean<BrandAdminListVO> getBrandAdminList(BrandAdminListQuery query) {

        int total = brandMapper.selectBrandAdminListCount(query);
        List<BrandAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = brandMapper.selectBrandAdminList(query);
            list.stream().forEach(b -> {
                b.setStatusText(b.getStatus().getText2());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public BrandAdminDetailVO getBrandAdminDetail(Long brandId) {

        Brand dbBrand = brandMapper.selectByPrimaryKey(brandId);
        state(dbBrand != null, "品牌详情数据异常");

        BrandAdminDetailVO detailVO = new BrandAdminDetailVO();
        BeanUtils.copyProperties(dbBrand, detailVO);
        detailVO.setBrandId(dbBrand.getId());
        detailVO.setLogoPath(dbBrand.getLogo());
        detailVO.setLogoSrc(fileService.getFileUrl(dbBrand.getLogo(), Constants.MALL_BUCKET_NAME));
        detailVO.setBannerPath(dbBrand.getDetailImage());
        detailVO.setBannerSrc(fileService.getFileUrl(dbBrand.getDetailImage(), Constants.MALL_BUCKET_NAME));

        return detailVO;
    }

    @Override
    @Transactional
    public void addBrand(Brand insertBrand) {

        Date time = new Date();
        if(CommonUtils.isEmpty(insertBrand.getWeight())) {
            insertBrand.setWeight(brandMapper.selectMaxWeight() + 1);
        }
        insertBrand.setStatus(CommonStatus.ENABLE);
        insertBrand.setCreateTime(time);
        insertBrand.setUpdateTime(time);
        brandMapper.insertSelective(insertBrand);
    }

    @Override
    @Transactional
    public void updateBrand(Brand updateBrand) {

        Date time = new Date();
        updateBrand.setUpdateTime(time);
        brandMapper.updateByPrimaryKeySelective(updateBrand);
    }

    @Override
    public List<Brand> getAllBrandList() {

        List<Brand> list = brandMapper.selectAllBrandList();

        return list;
    }
}
